//
//  FMDBJurisdiction.m
//  NursingManage
//
//  Created by Zhen Huang on 17/5/17.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "FMDBJurisdiction.h"
#import "FMDatabaseAdditions.h"
#import "HJurisdictionModel.h"
#import <TMCache.h>

static FMDBJurisdiction *manager = nil;

@implementation FMDBJurisdiction

//实现单例方法
+(FMDBJurisdiction *)sharedFMDBManeger {
    if (manager == nil) {
        manager = [[FMDBJurisdiction alloc]init];
    }
    return manager;
}

//准备好文件路径
- (NSString *)backFilePath {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *newFilePath = [documentPath stringByAppendingPathComponent:@"HJD.sqlite"];
    return newFilePath;//将新文件路径返回；
}

//创建数据库
- (void)dataBase {
    //创建数据库对象
    self.db = [FMDatabase databaseWithPath:[self backFilePath]];
    //打开数据库
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }
}

//创建表
- (void)createTable {
    [self.db open];
    NSString *sqlString = @"create table if not exists 'HJD'('group_name'text,'group_id'text)";
    BOOL result = [self.db executeUpdate:sqlString];
    NSLog(result ? @"创建成功":@"创建失败");
    [self.db close];
    
}

//添加数据
- (void)insertHJD:(HJurisdictionModel *)HJDModel{
    [self.db open];
    BOOL result = [self.db executeUpdate:@"insert into  HJD (group_name,group_id)values (?,?)",HJDModel.GROUP_NAME,HJDModel.GROUP_ID];
    NSLog(result ? @"添加成功" :@"添加失败");
    [self.db close];
}

//查询数据 相当于数据读取操作
- (NSMutableArray *)allModel {
    //执行查询语句，得到查询结果
    //    NSString *emp_id = [[TMCache sharedCache]objectForKey:@"emp_id"];
    //    NSString *sqlStr = [NSString stringWithFormat:@"select * from HSCDList where emp_id='%@'",emp_id];
    FMResultSet *set = [self.db executeQuery:@"select * from HJD"];
    NSMutableArray *array = [NSMutableArray array];
    while ([set next]) {
        HJurisdictionModel *model = [[HJurisdictionModel alloc]init];
        model.GROUP_NAME = [set stringForColumn:@"group_name"];
        model.GROUP_ID = [set stringForColumn:@"group_id"];
        [array addObject:model];
    }
    [self.db close];
    return array;
}


//删除数据
- (void)deleteHJD:(HJurisdictionModel *)HJDModel {
    [self.db open];
    BOOL result = [self.db executeUpdate:@"delete from HJD"];
    NSLog(result ? @"删除成功":@"删除失败");
    
    [self.db close];
}



@end
